unit ORM.Types.Fone;

interface

type
  TFone = record
  strict private
    FValue : string;
    FValorReferencia : string;
    function GetValue : string;
  private
    function FormataTelefone(pValue: string): string;
  public
    property Value : string read GetValue;

    function IsEmpty : Boolean;
    function GetFormatValue : string;
    procedure Clear;

    class operator Implicit(const pValue : string) : TFone;
    class operator Implicit(const pValue : Double) : TFone;
    class operator Implicit(const pValue : TFone) : string;
    class operator Implicit(const pValue : TFone) : Double;

    constructor Create(const pValue : string);
  end;

implementation

uses
  System.SysUtils, ORM.Utils, ORM.Types.Base;

{ TFone }

procedure TFone.Clear;
begin
  FValue := EmptyStr;
  FValorReferencia := EmptyStr;
end;

constructor TFone.Create(const pValue: string);
begin
  FValue := SomenteNumeros(pValue);
  FValorReferencia := CValorReferencia;
end;

class operator TFone.Implicit(const pValue: TFone): string;
begin
  Result := pValue.Value;
end;

class operator TFone.Implicit(const pValue: TFone): Double;
begin
  Result := StrToFloatDef(pValue.Value ,0);
end;

function TFone.FormataTelefone(pValue: string): string;
begin
  if Length(pValue) = 10 then
    Result := '(' + Copy(pValue, 1, 2) + ')' + Copy(pValue, 3, 4) + '-' + Copy(pValue, 7, 4);
end;

function TFone.GetFormatValue: string;
begin
  Result := FormataTelefone(FValue);
end;

function TFone.GetValue: string;
begin
  Result := FValue;
end;

class operator TFone.Implicit(const pValue: string): TFone;
begin
  Result := TFone.Create(pValue);
end;

class operator TFone.Implicit(const pValue: Double): TFone;
begin
  Result := TFone.Create(FloatToStr(pValue));
end;

function TFone.IsEmpty: Boolean;
begin
  Result := Length(FValorReferencia) = 0;
end;

end.
